<?php
	class Default_Model_DbTable_SiteLinks extends Zend_Db_Table_Abstract
	{
	    protected $_name = 'site_navigation';
	    protected $_sequence = '_seq_site_navigation';

	    protected $_primary = 'id';
	    protected $_pk = 'id';


	    public function getRow($id, $returnArray = false)
	    {
	        $id = (int)$id;
	    	$row = $this->fetchRow($this->_pk .'= '. $id);
	        if (!$row) {
	            throw new Exception("Count not find row $id");
	        }
	        if ($returnArray == true) {
		        return $row->toArray();
	        }
	        return $row;

	    }

	    public function getRows($filterArr=array(), $orderArr=array(), $returnArray = false)
	    {

	        $filter = $this->select();
	        if (!empty($filterArr)) {
		        foreach ($filterArr as $field_id=>$field_value) {
		        	if (!empty($field_value)) {
			    		$filter->where($field_id . ' = ?', $field_value);
		        	}
		        }
	        }

	        if (!empty($orderArr)) {
		        foreach ($orderArr as $field_id=>$orderType) {
			    	$filter->order($field_id .' '. $orderType);
		        }
	        } else {
	        	$filter->order($this->_pk .' DESC');
	        }
	        //echo $filter;exit;

			$rows = $this->fetchAll($filter);

	        if (!$rows) {
	            return array();
	        }
	        if ($returnArray == true) {
		        return $rows->toArray();
	        }
	        return $rows;

	    }

	    public function findRow($module, $controller, $action, $returnArray = false)
	    {
	    	$where = $this->select()
			        ->where('module_name = ?', $module)
			        ->where('controller_name = ?', $controller)
			        ->where('action_name = ?', $action)
			      	;

			      	//echo $where;
			$row = $this->fetchRow($where);
	    	//$row = $this->fetchRow('module= '. $module);
	        if (!$row) {
	            return false;
	        }
	        if ($returnArray == true) {
		        return $row->toArray();
	        }
	        return $row;

	    }

	    public function addRow($data)
	    {

	        $this->insert($data);
	        $id = $this->_db->lastInsertId($this->_name);
	        return $id;
	    }

	    public function updateRow($id, $data)
	    {
	        $this->update($data, $this->_pk . ' = ' . (int)$id);

	    }

	    public function deleteRow($id)
	    {
	        $this->delete($this->_pk .'=' . (int)$id);
	    }

		public function getSiteActions($module='', $controller='') {

			$front = Zend_Controller_Front::getInstance();
			//$front = $this->getFrontController();
		    $siteActions = array();

		    $filter = new Zend_Filter_Word_CamelCaseToDash();

	        foreach ($front->getControllerDirectory() as $module => $path) {
				$tmpPath = scandir($path);

	            foreach ($tmpPath as $file) {

                    if (strstr($file, "Controller.php") !== false) {

                        include_once $path . DIRECTORY_SEPARATOR . $file;
                        foreach (get_declared_classes() as $class) {
                            if (is_subclass_of($class, 'Zend_Controller_Action')) {
                                $controller = substr($class, 0, strpos($class, "Controller"));
                                //$controller = strtolower($filter->filter($controller));
                                $actions = array();

                                foreach (get_class_methods($class) as $action) {
                                    if (strstr($action, "Action") !== false) {
                                    	$action = (substr($action, 0, strpos($action, "Action")));
                                    	$action = strtolower($filter->filter($action));
										$actions[] = $action;
                                    }
                                }
                            }
                        }
                        $siteActions[$module][$controller] = $actions;
                    }
            	}
	        }
	        return $siteActions;
		}

	}